package cn.zhouyafeng.itchat4j.thread;

import cn.zhouyafeng.itchat4j.core.MultiCore;
import cn.zhouyafeng.itchat4j.utils.SleepUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.Callable;

/**
 * \* Created with IntelliJ IDEA.
 * \* User: ZhaoTao
 * \* Date: 2017/12/11
 * \* Time: 10:15
 * \* Description:
 * \
 */
public class MultiCheckLoginStatusThread  implements Callable {
    private static Logger LOG = LoggerFactory.getLogger(CheckLoginStatusThread.class);
    private MultiCore core = null;

    public MultiCheckLoginStatusThread(String key) {
        core = MultiCore.getInstance(key);
    }

    @Override
    public Boolean call() {
        while (core.isAlive()) {
            long t1 = System.currentTimeMillis(); // 秒为单位
            if (t1 - core.getLastNormalRetcodeTime() > 60 * 1000) { // 超过60秒，判为离线
                core.setAlive(false);
                LOG.info("微信已离线");
                return true;
            }
            SleepUtils.sleep(10 * 1000); // 休眠10秒
        }
        return false;
    }

}
